package models;

import java.util.*;

import javax.persistence.*;

import controllers.SocialProfiles;
import controllers.Secure.Security;

import play.data.validation.*;
import play.db.jpa.*;

@Entity
public class AbstractProfile extends Model
{

	@Required
	public String				name;

	// bi-directional one-to-many, inverse side
	@OneToMany(mappedBy = "abstractProfile", cascade = CascadeType.ALL, orphanRemoval = true)
	public List<SocialProfile>	profileList;

	// bi-directional one-to-many, owning side
	@ManyToOne
	public User					user;

	public boolean				inConnection;

	/**
	 * Constructor to create an abstract profile
	 * 
	 * @param name
	 *            : String name of the abstract profile
	 * @param profileList
	 *            : list of social profiles (OSN accounts) connected to this
	 *            abstract profile
	 * @param user
	 *            : the owning side of this abstract profile
	 */
	public AbstractProfile(String name, List<SocialProfile> profileList,
			User user)
	{
		super();
		this.name = name;
		this.profileList = profileList;
		this.user = user;
		inConnection = false;
	}

	/**
	 * Method to find an abstract profile from the database
	 * 
	 * @param username
	 *            : String name of the abstract profile to find
	 * @return this method returns the abstract profile if found or null if
	 *         there is no abstract profile with a name matching the search
	 *         query
	 */
	public AbstractProfile findByName(String name)
	{
		List<AbstractProfile> absProfiles = AbstractProfile.findAll();
		for (int i = 0; i < absProfiles.size(); i++)
		{
			AbstractProfile temp = (AbstractProfile) absProfiles.get(i);
			if (temp.name.equalsIgnoreCase(name))
			{
				return temp;
			}
		}
		return null;
	}

}